package com.easy.controller;

import com.easy.bean.CheckIns;
import com.easy.bean.Guests;
import com.easy.bean.Rooms;
import com.easy.common.CommonResult;
import com.easy.dao.ICheckInksDao;
import com.easy.service.ICheckIns;
import com.easy.service.IGuests;
import com.easy.service.IRooms;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class CheckInsController {
    @Autowired
    ICheckInksDao dao;

    @Autowired
    ICheckIns easyC;

    @Autowired
    IGuests easyG;

    @Autowired
    IRooms easyR;

    //添加
    @PostMapping("checkinsjson")
    public CommonResult addCheckInsJson( CheckIns checkIns, Guests guests, Rooms rooms){
        easyG.addGuests(guests);
        easyR.editRooms_check(rooms);
        easyC.addCheckIns(checkIns);
        checkIns=easyC.getCheckIns(checkIns.getId());
        return CommonResult.success(checkIns);
    }

    @PostMapping("checkinsjson_re")
    public CommonResult addCheckInsJson_re( CheckIns checkIns){
        easyC.addCheckIns(checkIns);
        checkIns=easyC.getCheckIns(checkIns.getId());
        return CommonResult.success(checkIns);
    }
    //删除
    @DeleteMapping("checkins/{id}")
    public CommonResult delCheckIns(@PathVariable int id,Guests guests,Rooms rooms){
        easyG.delGuests(guests);
        easyR.editRooms_check(rooms);
        int count=easyC.delCheckIns(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }
    //修改
    @PutMapping("checkinsjson")
    public CommonResult editCheckIns(CheckIns checkIns,Guests guests){
        easyG.editGuests_check(guests);
        System.out.println(guests.getNumber());
        int count=easyC.editCheckIns(checkIns);
        checkIns=easyC.getCheckIns(checkIns.getId());
        return CommonResult.success(checkIns);
    }
    //查询
    @GetMapping("searchcheckins")
    public CommonResult searchCheckIns(CheckIns checkIns, Page page){
        List<CheckIns> list=easyC.searchCheckIns(checkIns,page);
        int count=easyC.getCount(checkIns);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }
}
